package com.pool.activity;

import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.pool.R;
import com.pool.activity.common.FullscreenListActivity;
import com.pool.common.Lists;
import com.pool.common.Maps;
import com.pool.common.Shoot;
import com.pool.common.Strings;
import com.pool.store.ShootStore;
import com.pool.store.Store;

/**
 * @author claveld@gmail.com (David Clavel)
 */
public class SelectShoot extends FullscreenListActivity {

  public static final String PLAYER = "{player}";
  
  private static final String[] REMAINING_BALLS = {"0", "1", "2", "3", "4", "5", "6", "7"};
  
  private final Store<Shoot> shootStore;

  public SelectShoot() {
    this(new ShootStore());
  }
  
  public SelectShoot(Store<Shoot> shootStore) {
    this.shootStore = shootStore;
  }
  
  public static Intent createIntentForPlayer(Context context, String player) {
    Intent result = new Intent(context, SelectShoot.class);
    result.putExtra(SelectShoot.PLAYER, player);
    return result;
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.local_store);
    
    String player = getIntent().getExtras().getString(SelectShoot.PLAYER);
    setTitle(Strings.getString(R.string.choose_shot_for).replace(SelectShoot.PLAYER, player));
    
    List<Map<String, Object>> myData = Lists.newArrayList();
    for (Shoot shoot : shootStore.getAll()) {
      addItem(myData, shoot);
    }

    setListAdapter(new SimpleAdapter(
        this,
        myData,
        R.layout.two_line_row,
        new String[] { "line1", "line2" },
        new int[] { R.id.text1, R.id.text2 }));
    getListView().setTextFilterEnabled(true);
    getListView().setDividerHeight(2);
  }
  
  @SuppressWarnings("unchecked")
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    Map<String, Object> map = (Map<String, Object>) l.getItemAtPosition(position);
    Shoot shoot = (Shoot) map.get("shoot");
    getIntent().putExtra("shoot", shoot.id);
    getIntent().putExtra("balls", -1);
    
    if (shoot.isVictory()) {
      Builder builder = new AlertDialog.Builder(this);
      builder.setTitle(R.string.remaining_balls);
      builder.setItems(REMAINING_BALLS, new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          getIntent().putExtra("balls", which);
          returnAndFinish();
        }
      });
      builder.create().show();
    } else {
      returnAndFinish();
    }
  }
  
  private void returnAndFinish() {
    setResult(RESULT_OK, getIntent());
    finish();
  }

  private void addItem(List<Map<String, Object>> list, Shoot shoot) {
    Map<String, Object> map = Maps.newHashMap();
    map.put("line1", shoot.description);
    map.put("line2", "Points: " + shoot.bonus);
    map.put("shoot", shoot);
    list.add(map);
  }
}
